#include "ChatServiceImpl.h"
#include "UserMgr.h"
#include "const.h"
#include <json/json.h>
#include <json/value.h>
#include <json/reader.h>
#include "CSession.h"


// 通知添加好友
Status ChatServiceImpl::NotifyAddFriend(ServerContext* context, const AddFriendReq* request, AddFriendRsp* response)
{
    response->set_error(ErrorCodes::Success);
    // 服务器收到添加好友请求后，找到目标添加好友的TCP长链接
    // 首先查看目标添加好友的TCP长链接是否存在
    std::shared_ptr<CSession> session = UserMgr::GetInstance()->GetSession(request->touid());
    if(session == nullptr)
    {
        response->set_error(ErrorCodes::RPCFailed);
        return Status::OK;
    }

    Defer defer([&request,&response](){
        response->set_applyuid(request->applyuid());
        response->set_touid(request->touid());
    });

    /*
        //申请添加好友的用户id
    int32 applyuid = 1;
    //申请添加好友的用户的昵称
    string name=2;
    //申请添加好友的用户描述
    string desc=3;
    //目标添加好友的用户id
    int32 touid=4;
    // 申请人的icon
    string icon=5;
    // 被申请人的nick
    string nick=6;
    // 性别
    int32 sex=7;
    */
    // 给客户端推送添加好友请求
    Json::Value msg;
    msg["error"] = ErrorCodes::Success;
    msg["applyuid"] = request->applyuid();
    msg["desc"] = request->desc();
    msg["name"] = request->name();
    msg["icon"] = request->icon();
    msg["nick"] = request->nick();
    msg["sex"] = request->sex();

    session->Send(msg.toStyledString(),MSG_IDS::ID_NOTIFY_ADD_FRIEND_REQ);

    return Status::OK;
}
// 回应添加好友请求
Status ChatServiceImpl::ReplyAddFriend(ServerContext* context, const ReplyFriendReq* request, ReplyFriendRsp* response)
{
    return Status::OK;
}
// 发送聊天消息
Status ChatServiceImpl::SendChatMsg(ServerContext* context, const SendChatMsgReq* request, SendChatMsgRsp* response)
{
    return Status::OK;
}
// 通知验证好友后给对端通知
Status ChatServiceImpl::NotifyAuthFriend(ServerContext* context, const AuthFriendReq* request, AuthFriendRsp* response)
{
    return Status::OK;
}
Status ChatServiceImpl::NotifyTextChatMsg(ServerContext* context, const TextChatMsgReq* request, TextChatMsgRsp* response)
{
    return Status::OK;
}


bool ChatServiceImpl::GetBaseInfo(int uid, std::shared_ptr<UserInfo>& userInfo)
{
    return true;
}